/*******************************************************************************
 * Copyright (c) 2003, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.sg.business.commons.ui.flowchart.part;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.graph.CompoundDirectedGraph;
import org.eclipse.draw2d.graph.Edge;
import org.eclipse.draw2d.graph.Node;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editparts.AbstractConnectionEditPart;
import org.eclipse.gef.editpolicies.ConnectionEndpointEditPolicy;

import com.sg.business.commons.ui.flowchart.policies.TransitionEditPolicy;
import com.sg.business.commons.ui.flowchart.router.AIConnectionRouter;

/**
 * @author hudsonr Created on Jul 16, 2003
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class TransitionPart extends AbstractConnectionEditPart {

	private List<IFigure> ifigures;

	private List<Rectangle> rectangles;

	protected void applyGraphResults(CompoundDirectedGraph graph, Map map) {
		if (ifigures == null) {
			ifigures = new ArrayList<IFigure>();
			rectangles = new ArrayList<Rectangle>();
			Set keySet = map.keySet();
			Iterator iterator = keySet.iterator();
			while (iterator.hasNext()) {
				Object next = iterator.next();
				if (next instanceof SimpleActivityPart) {
					SimpleActivityPart nodeActivity = (SimpleActivityPart) next;
					IFigure figure2 = nodeActivity.getFigure();
					rectangles.add(figure2.getBounds());
				}
			}
		}

		PolylineConnection conn = (PolylineConnection) getConnectionFigure();
		conn.setConnectionRouter(new AIConnectionRouter(rectangles));
		conn.setTargetDecoration(new PolygonDecoration());
		// Edge e = (Edge) map.get(this);
		// NodeList nodes = e.vNodes;
		// if (nodes != null) {
		// List bends = new ArrayList();
		// for (int i = 0; i < nodes.size(); i++) {
		// Node vn = nodes.getNode(i);
		// int x = vn.x;
		// int y = vn.y;
		// if (e.isFeedback()) {
		// bends.add(new AbsoluteBendpoint(x, y + vn.height));
		// bends.add(new AbsoluteBendpoint(x, y));
		// } else {
		// bends.add(new AbsoluteBendpoint(x, y));
		// bends.add(new AbsoluteBendpoint(x, y + vn.height));
		// }
		// }
		// conn.setRoutingConstraint(bends);
		// } else {
		// conn.setRoutingConstraint(Collections.EMPTY_LIST);
		// }
	}

	/**
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE,
				new ConnectionEndpointEditPolicy());
		installEditPolicy(EditPolicy.CONNECTION_ROLE,
				new TransitionEditPolicy());
	}

	/**
	 * @see org.eclipse.gef.editparts.AbstractConnectionEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		PolylineConnection conn = (PolylineConnection) super.createFigure();
		// conn.setConnectionRouter(new BendpointConnectionRouter() {
		// public void route(Connection conn) {
		// GraphAnimation.recordInitialState(conn);
		// if (!GraphAnimation.playbackState(conn))
		// super.route(conn);
		// }
		// });

		// conn.setTargetDecoration(new PolygonDecoration());

		// conn.setConnectionRouter(new ManhattanConnectionRouter());
		//
		// conn.setTargetDecoration(new PolygonDecoration());
		//
		// conn.setForegroundColor(Widgets.getColor(null, 127, 127, 127));
		//
		// conn.setLineWidth(2);
		return conn;
	}

	/**
	 * @see org.eclipse.gef.EditPart#setSelected(int)
	 */
	public void setSelected(int value) {
		super.setSelected(value);
		if (value != EditPart.SELECTED_NONE)
			((PolylineConnection) getFigure()).setLineWidth(2);
		else
			((PolylineConnection) getFigure()).setLineWidth(1);
	}

	public void contributeToGraph(CompoundDirectedGraph graph, Map map) {
		GraphAnimation.recordInitialState(getConnectionFigure());
		Node source = (Node) map.get(getSource());
		Node target = (Node) map.get(getTarget());
		Edge e = new Edge(this, source, target);
		e.weight = 2;
		graph.edges.add(e);
		map.put(this, e);
	}

}
